<?php
namespace app\com\simtoon\comm;

use Yii;
use yii\base\Exception;
use yii\data\Pagination;


defined('REGION') or define('REGION',"rg_") ;
/**
 * 通用类
 */
class Common
{
	/**
	 * @var Singleton
	 */
	private static $_instance = null;


	/**
	 *@var config
	*/
	private $config = array();

	/**
	 * Common
	*/
	private function __construct()
	{

	}

	/**
	 * Singleton Pattern
	 * 鍗曚緥妯″紡
	 */
	public static function getCommonInstance()
	{
		if(is_null(self::$_instance))
			self::$_instance = new Common();
		return self::$_instance;
	}
	
	
	/**
	 * 濡傛灉灞炴�鍊糲onfig涓虹┖鏁扮粍锛岄渶瑕佽В鏋恑ni閰嶇疆鏂囦欢缁欏睘鎬onfig
	 */
	private function getConfigIni()
	{
		if(empty($this->config))
		{
			$this->config = parse_ini_file(CONFIG_PATH);
		}
	}


	/**
	 * 鑾峰緱缂撳瓨椤圭殑key鐨勭畝鍐�
	 * @param string $key key鐨勫悕绉�
	 * @return string key鐨勭畝鍐�
	 */
	public static function getSimplePrefix($key){
		try{
			$format = Common::$cacheFormat;
			return $format[$key];
		}catch(Exception $e){
			self::log($e->getMessage(), "Common.php  getSimplePrefix");
			return "";
		}
			
	}

	/**
	 * 获得客户端IP
	 */
	public static function getClientIP(){
		if(!empty($_SERVER["HTTP_CLIENT_IP"]))
			$cip = $_SERVER["HTTP_CLIENT_IP"];
		else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
			$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		else if(!empty($_SERVER["REMOTE_ADDR"]))
			$cip = $_SERVER["REMOTE_ADDR"];
		else
			$cip = false;
		return $cip;
	}
	
	/**
	 * 获得随机ID
	 * @param int $len 长度
	 * @param string $type 类型
	 * @return string 随机数
	 */
	public static function getRandomID($len, $type="1")
	{
		$numArr = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
		$upperArr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' );
		$lowerArr = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' );
		$randStr = "";
		if($type == "1")
			$randArr = array_merge($numArr, $upperArr, $lowerArr);
		else if($type == "2")
			$randArr = array_merge($upperArr, $lowerArr);
		else if($type == "3")
			$randArr = $numArr;
		$cnt = count($randArr);
		for( $i = 0; $i < $len; $i++ )
			$randStr .= $randArr[mt_rand(0, $cnt-1)];
		return $randStr;
	}


	public static function getCondString($condBy,$fieldMap,$strField)
	{
		$condString = null;
		$conditionArr = json_decode($condBy,true);
		if(count($conditionArr)>0){
			$condString = " WHERE ";
			$aliasArr = $fieldMap;
			foreach ($conditionArr as $key=>$item){
				$item = self::checkCondStr($item);
				if(isset($aliasArr[$key])){
					if(in_array($aliasArr[$key],$strField))
						$item = "'".$item."'";
					$condString .= $aliasArr[$key] . "=" . $item . " AND ";
				}else{
					break;
				}
			}
			if(strlen($condString) > 0 ) 
				$condString = substr($condString,0,strlen($condString) - 4);
		}
		return $condString;
	}
	
	//sql娉ㄥ叆妫�煡
	public static function checkCondStr($condStr)
	{
		if (!get_magic_quotes_gpc()) // 鍒ゆ柇magic_quotes_gpc鏄惁鎵撳紑
		{
			$condStr = addslashes($condStr); // 杩涜杩囨护
		}
		$condStr = str_replace("_", "\\_", $condStr); // 鎶�'_'杩囨护鎺�
		$condStr = str_replace("%", "\\%", $condStr); // 鎶� % '杩囨护鎺�
		 
		return $condStr;
	}
	
	/**
	 * 获得分页对象
	 * @param integer $count 总数
	 * @param integer $page 页数
	 * @param integer $prePage 每页显示数
	 * @return \yii\data\Pagination  分页对象
	 */
	public static function getPagination($count,$page,$prePage)
	{
		$pages = new Pagination(['totalCount' => $count,
				'pageSizeLimit' => [$page,$prePage]]);
		$pages->setPage($prePage);
		$pages->setPageSize($page);
		return $pages;
	}
	
	/**
	 * 获得时间毫秒数
	 * @return string 毫秒数
	 */
	public static function getMicrotimeWithInt(){
		$timeArr = explode(" ",microtime());
		return $timeArr[1].($timeArr[0]*100000000);
	}

	/**
	 * 获得远程访问IP
	 * @return string 远程IP
	 */
	public static function getRemoteIP(){
		if(!empty($_SERVER["REMOTE_ADDR"]))
			$cip = $_SERVER["REMOTE_ADDR"];
		else
			$cip = false;
		return $cip;
	}
}